package com.sduept.nwld.dataserver.rest.daily;

import com.sduept.cim.model.core.Substation;
import com.sduept.core.dao.SuperDAO;
import com.sduept.bigdata.weather.map.MapDataCache;
import com.sduept.nwld.dataserver.manager.protection.ProtectOverhaulManager;
import com.sduept.nwld.dataserver.model.daily.OverhaulMsg;
import com.sduept.utils.DateUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 保护检修管理
 *
 * @author zq
 */
@RestController
@RequestMapping("/protectOverhaul")
public class ProtectOverhaulService {

    @Autowired
    private ProtectOverhaulManager pom;
    @Autowired
    private SuperDAO dao;
    @Autowired
    private MapDataCache cc;

    /**
     * 查询检修计划，用于保护检修地图模块展示
     *
     * @param start 已完工数据的“结束时间”的开始
     * @param end   已完工数据的“结束时间“的结束
     * @return
     */

    @GetMapping("/getMapProtectOverhaul/{start}/{end}")
    public String getMapProtectOverhaul(@PathVariable("start") String start, @PathVariable("end") String end) {
        //查询开始时间为今天的待开工数据
        List<OverhaulMsg> todo = pom.getTodoOverhaulMsgByStartDate(new Date());
        List<OverhaulMsg> doing = pom.getDoingOverhaulMsg();

        Date s = DateUtil.getDateByString(start, DateUtil.pattern_ymd);
        Date e = DateUtil.getDateByString(end, DateUtil.pattern_ymd);
        Calendar c = Calendar.getInstance();
        c.setTime(e);
        c.add(Calendar.DAY_OF_YEAR, 1);
        e = c.getTime();
        List<OverhaulMsg> done = pom.getDoneOverhaulMsgByDate(s, e);
        JSONObject overhauls = new JSONObject();
        overhauls.put("todo", addLonLat(todo));
        overhauls.put("doing", addLonLat(doing));
        overhauls.put("done", addLonLat(done));
        return overhauls.toString();
    }

    /**
     * 查询检修计划，用于保护检修地图弹窗模块展示
     *
     * @param start 已完工数据的“结束时间”的开始
     * @param end   已完工数据的“结束时间“的结束
     * @return
     */

    @GetMapping("/getProtectOverhaulByDate/{start}/{end}")
    public String getDefaultProtectOverhaul(@PathVariable("start") String start, @PathVariable("end") String end) {
        List<OverhaulMsg> todo = pom.getTodoOverhaulMsgByStartDate(new Date());
        List<OverhaulMsg> doing = pom.getDoingOverhaulMsg();
        Date startDate = DateUtil.getDateByString(start + " 00:00:00", DateUtil.pattern_ymdhms);
        Date endDate = DateUtil.getDateByString(end + " 23:59:59", DateUtil.pattern_ymdhms);
        List<OverhaulMsg> done = pom.getDoneOverhaulMsgByDate(startDate, endDate);
        todo.addAll(doing);
        todo.addAll(done);
        return JSONArray.fromObject(todo).toString();
    }

    /**
     * 查询检修计划，用于保护检修地图单变电站弹窗模块展示
     *
     * @param start     已完工数据的“结束时间”的开始
     * @param end       已完工数据的“结束时间“的结束
     * @param stationId 工作地点的id
     * @return
     */

    @GetMapping("/getProtectOverhaulByDate/{start}/{end}/{stationId}")
    public String getDefaultProtectOverhaul(@PathVariable("start") String start, @PathVariable("end") String end,
                                            @PathVariable("stationId") String stationId) {
        List<OverhaulMsg> todo = pom.getTodoOverhaulMsg(stationId, new Date());
        List<OverhaulMsg> doing = pom.getDoingOverhaulMsg(stationId);
        Date startDate = DateUtil.getDateByString(start + " 00:00:00", DateUtil.pattern_ymdhms);
        Date endDate = DateUtil.getDateByString(end + " 23:59:59", DateUtil.pattern_ymdhms);
        List<OverhaulMsg> done = pom.getDoneOverhaulMsgByDate(startDate, endDate, stationId);
        todo.addAll(doing);
        todo.addAll(done);
        return JSONArray.fromObject(todo).toString();
    }

    private JSONArray addLonLat(List<OverhaulMsg> overhauls) {
        JSONArray arr = new JSONArray();
		Map<String, Substation> stationMap = cc.getStationid_map();
        for (OverhaulMsg o : overhauls) {
            JSONObject jo = JSONObject.fromObject(o);
			Substation st = stationMap.get(o.getStation_id());
            if (st != null) {
                jo.put("lon", st.getLongitude());
                jo.put("lat", st.getLatitude());
            }
            arr.add(jo);
        }
        return arr;
    }

}
